import {MathModel} from "@/model/MathModel";
import {shuffleArray} from "@/util/yml";
import { mathType } from "@/constant/mathConstants";

export const generatorMath = (params: any):MathModel[] => {
    const pmathType = params.mathType
    const mathRangeType = params.mathRangeType
    const generatorCount = params.generatorCount
    let max = 100
    // 必须是整数
    const mustInteger = true
    switch (mathRangeType) {
        case 1:{
            max = 10
            break
        }
        case 2:{
            max = 20
            break
        }
        case 3:{
            max = 100
            break
        }
        // case 4:{
        //     max = 1000
        //     break
        // }
        default:{
            break
        }
    }
    // 九九乘法表
    if (pmathType.length === 1 && pmathType[0] === 4){
        const arr:MathModel[] = []
        for (let i = 1; i <= 9; i++) {
            for (let j = 1; j <= 9; j++) {
                const model = new MathModel(i,j,pmathType)
                model.getMath()
                arr.push(model)
            }
        }
        const res = []
        const newArray = shuffleArray(arr)
        if (newArray.length > generatorCount){
            return newArray.slice(0, generatorCount)
        }else if (newArray.length == generatorCount){
            return newArray
        } else {
            res.push(...newArray)
            while (res.length < generatorCount){
                const a = shuffleArray(arr)
                res.push(...a)
            }
            if (res.length > generatorCount){
                return res.slice(0, generatorCount)
            }
            return res;
        }
    }
    console.log("是否整数：",mustInteger)
    const arr:MathModel[] = []
    // const arrStr:string[] = []
    // 生成
    while (arr.length < generatorCount){
        const currMathType = pmathType[getRandomInt(0,pmathType.length-1)]
        const p1 = getRandomInt(0,max)
        const p2 = getRandomInt(0,max)
        const model = new MathModel(p1,p2,currMathType)
        model.getMath()
        if(currMathType == 6){
            arr.push(model)
        }else {
            if (model.result!! < 0 || model.result!! > max){
                continue
            }
            if (mustInteger && Number.isInteger(model.result) && params.mathItemCount === 2){
                arr.push(model)
            }else{
                const pp1 = model.result!!
                const pp3 = getRandomInt(0,max)
                const cMathType = pmathType[getRandomInt(0,pmathType.length-1)]
                const model1 = new MathModel(pp1,pp3,cMathType)
                model1.getMath()
                if (model1.result!! < 0 || model1.result!! > max){
                    continue
                }
                if (mustInteger && Number.isInteger(model.result)){
                    // const res = new MathModel(p1,p2,currMathType)
                    model.param3 = pp3
                    model.result = model1.result
                    model.type2 = cMathType
                    model.typeStr2 = mathType[cMathType-1].str
                    arr.push(model)
                }
            }
        }

        
    }
    return arr
}

function generatorMathModel (max:number,currMathType:any,mustInteger:boolean){
    const p1 = getRandomInt(0,max)
    const p2 = getRandomInt(0,max)
    const model = new MathModel(p1,p2,currMathType)
    model.getMath()
    if (model.result!! < 0 || model.result!! > max){
        return null
    }
    if (mustInteger && Number.isInteger(model.result)){
        return model
    }
    return null
}


function getRandomInt(min: number, max:number) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
